/**
 * 
 */

NotificationFullPortlet = Class.extend(
		{
			init : function() {
				this.name = "NotificationFullPortlet";
				this.model = {};
			},

			doAjax : function() {
				var root = SingletonFactory.getInstance(Application)
						.getSystemProperties().get("host.root");
				var currentPltObj = this;
				// get notification 
				$.get(root + "ajax/get-notification-full/", {},
						function(ret) {
							ret = $.parseJSON(ret);
							ret = ret.result;
							if(ret == 'error' || ret == undefined){
								// Navigate to error page
								var subject = SingletonFactory.getInstance(Subject);
								var request = new Request("ErrorPage","donganh",{"code":"888", "msg":"Lỗi","content":"Lỗi tự tạo thôi"}); 
								subject.notifyEvent('RequestRoute', request);
							} else {
								ret = $.parseJSON(ret);
								currentPltObj.afterAjax(ret);
							}
						});
			},

			afterAjax : function(ret) {
				// {profilename:name, profilepic: pic, profileid: id, message:
				// message, detailid: detailID}
				// loop and build template
				var notifList = Array();
				var num = 0;
				for ( var i in ret) {
					var objParam = {};
					objParam.profilename = ret[i]['name'];
					objParam.profilepic = ret[i]['avatar'];
					objParam.profileid = ret[i]['id'];
					objParam.message = ret[i]['content'];
					objParam.link = this.buildLink(eval("("+ret[i]['link'] + ")"));
					objParam.detail = "";
					var template = this.renderView("NotifUnSeen", objParam);
					notifList.push(template);
					num++;
				}
				var header = this.getLocalizedMessage('NoNotif', num);
				this.model.noNotif = header;
				this.model.messageNoNotif = header;
				this.model.children = notifList;
				this.getPortletPlaceholder().drawToCanvas(this.render());
			},
			
			buildLink: function(linkJson){
				var linkResult = "";
				for ( var i in linkJson) {
					var key = i;
					var value = linkJson[key];
					if (key == 'type')	{
						key = 'page';
						if (value == 'Question')	{
							value = 'Question';
						}
					}
					linkResult += "/" + key + "/" + value;
				}
				var result = "#" + linkResult.substring(1);;
				return result;
			},

			run : function() {
				this.doAjax();
			}
		}).implement(RenderInterface).implement(PortletInterface);